package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {

    @Autowired
    private SmallVideosService videosService;

    /**
     * 发布视频  接口路径：POST  请求参数： videoThumbnail：封面图  videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity savaVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws Exception {
        videosService.savaVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 关注发布视频的作者
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long uid) {
        videosService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注视频作者
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("id") Long followUserId) {
        videosService.userUnFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频点赞功能
     */
    @PostMapping("/{id}/like")
    public ResponseEntity videosLike(@PathVariable("id") String id) {
        Integer counts = videosService.likeVideoCounts(id);
        return ResponseEntity.ok(counts);
    }

    /**
     * 取消视频点赞功能
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String videoId){
        Integer likeCount = videosService.dislikeVideoComments(videoId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 发布视频评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@RequestBody Map map) {
        String videoId = (String) map.get("id");
        String comment = (String) map.get("comment");
        videosService.saveComments(videoId, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 查看视频评论
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10")Integer pagesize,
                                       @PathVariable("id") String videoId) {
        PageResult pr = videosService.findComments(page,pagesize,videoId);
        return ResponseEntity.ok(pr);
    }

    /**
     *  视频评论点赞功能
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable("id") String commentsId){
        videosService.commentsLike(commentsId);
        return ResponseEntity.ok(null);
    }

    /**
     *  视频评论点赞取消功能
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity disCommentsLike(@PathVariable("id") String commentsId){
        videosService.disCommentsLike(commentsId);
        return ResponseEntity.ok(null);
    }
}